home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GP_UNIX.C < prev    next >
C/C++ Source or Header  |  1992-03-20  |  7KB  |  233 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gp_unix.c */
  21. /* Unix-specific routines for Ghostscript */
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "gx.h"
  25. #include "gp.h"
  26. #include "stat_.h"
  27. #include "time_.h"
  28.  
  29. /* popen isn't POSIX-standard, so we declare it here. */
  30. extern FILE *popen();
  31. extern int pclose();
  32.  
  33. /* Do platform-dependent initialization */
  34. void
  35. gp_init()
  36. {
  37. }
  38.  
  39. /* ------ Date and time ------ */
  40.  
  41. /* Read the current date (in days since Jan. 1, 1980) */
  42. /* and time (in milliseconds since midnight). */
  43. void
  44. gp_get_clock(long *pdt)
  45. {    long secs_since_1980;
  46.     struct timeval tp;
  47.     struct timezone tzp;
  48.     time_t tsec;
  49.     struct tm *tm, *localtime();
  50.  
  51.     if ( gettimeofday(&tp, &tzp) == -1 )
  52.        {    perror("Ghostscript: gettimeofday failed:");
  53.         exit(-1);
  54.        }
  55.  
  56.     /* tp.tv_sec is #secs since Jan 1, 1970 */
  57.  
  58.     /* subtract off number of seconds in 10 years */
  59.     /* leap seconds are not accounted for */
  60.     secs_since_1980 = tp.tv_sec - (long)(60 * 60 * 24 * 365.25 * 10);
  61.  
  62.     /* adjust for timezone */
  63.     secs_since_1980 -= (tzp.tz_minuteswest * 60);
  64.  
  65.     /* adjust for daylight savings time - assume dst offset is 1 hour */
  66.     tsec = tp.tv_sec;
  67.     tm = localtime(&tsec);
  68.     if ( tm->tm_isdst )
  69.         secs_since_1980 += (60 * 60);
  70.  
  71.     /* divide secs by #secs/day to get #days (integer division truncates) */
  72.     pdt[0] = secs_since_1980 / (60 * 60 * 24);
  73.     /* modulo + microsecs/1000 gives number of millisecs since midnight */
  74.     pdt[1] = (secs_since_1980 % (60 * 60 * 24)) * 1000 + tp.tv_usec / 1000;
  75. #ifdef DEBUG_CLOCK
  76.     printf("tp.tv_sec = %d  tp.tv_usec = %d  pdt[0] = %ld  pdt[1] = %ld\n",
  77.         tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
  78. #endif
  79. }
  80.  
  81. /* ------ Screen management ------ */
  82.  
  83. /* Write a string to the console. */
  84. void
  85. gp_console_puts(const char *str, uint size)
  86. {    fwrite(str, 1, size, stdout);
  87. }
  88.  
  89. /* Make the console current on the screen. */
  90. int
  91. gp_make_console_current(struct gx_device_s *dev)
  92. {    return 0;
  93. }
  94.  
  95. /* Make the graphics current on the screen. */
  96. int
  97. gp_make_graphics_current(struct gx_device_s *dev)
  98. {    return 0;
  99. }
  100.  
  101. /* ------ Printer accessing ------ */
  102.  
  103. /* Open a connection to a printer.  A null file name means use the */
  104. /* standard printer connected to the machine, if any. */
  105. /* "|command" opens an output pipe. */
  106. /* Return NULL if the connection could not be opened. */
  107. FILE *
  108. gp_open_printer(char *fname)
  109. {    return
  110.       (strlen(fname) == 0 ?
  111.        gp_open_scratch_file(gp_scratch_file_name_prefix, fname, "wb") :
  112.        fname[0] == '|' ?
  113.        popen(fname + 1, "wb") :
  114.        fopen(fname, "wb"));
  115. }
  116.  
  117. /* Close the connection to the printer. */
  118. void
  119. gp_close_printer(FILE *pfile, const char *fname)
  120. {    if ( fname[0] == '|' )
  121.         pclose(pfile);
  122.     else
  123.         fclose(pfile);
  124. }
  125.  
  126. /* ------ File names ------ */
  127.  
  128. /* Define the character used for separating file names in a list. */
  129. const char gp_file_name_list_separator = ':';
  130.  
  131. /* Define the default scratch file name prefix. */
  132. const char gp_scratch_file_name_prefix[] = "/tmp/gs_";
  133.  
  134. /* Define whether case is insignificant in file names. */
  135. const int gp_file_names_ignore_case = 0;
  136.  
  137. /* Create and open a scratch file with a given name prefix. */
  138. /* Write the actual file name at fname. */
  139. FILE *
  140. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  141. {    strcpy(fname, prefix);
  142.     strcat(fname, "XXXXXX");
  143.     mktemp(fname);
  144.     return fopen(fname, mode);
  145. }
  146.  
  147. /* Answer whether a file name contains a directory/device specification, */
  148. /* i.e. is absolute (not directory- or device-relative). */
  149. int
  150. gp_file_name_is_absolute(const char *fname, uint len)
  151. {    /* A file name is absolute if it starts with a /. */
  152.     return ( len >= 1 && *fname == '/' );
  153. }
  154.  
  155. /* Answer the string to be used for combining a directory/device prefix */
  156. /* with a base file name.  The file name is known to not be absolute. */
  157. char *
  158. gp_file_name_concat_string(const char *prefix, uint plen,
  159.                const char *fname, uint len)
  160. {    if ( plen > 0 && prefix[plen - 1] == '/' )
  161.         return "";
  162.     return "/";
  163. }
  164.  
  165. /* ------ File operations ------ */
  166.  
  167. /* If the file given by fname exists, fill in its status and return 1; */
  168. /* otherwise return 0. */
  169. int
  170. gp_file_status(const char *fname, file_status *pstatus)
  171. {    struct stat sbuf;
  172.     /* The RS/6000 prototype for stat doesn't include const, */
  173.     /* so we have to explicitly remove the const modifier. */
  174.     if ( stat((char *)fname, &sbuf) < 0 ) return 0;
  175.     pstatus->size_pages = stat_blocks(&sbuf);    /* st_blocks is */
  176.                     /* missing on some systems, */
  177.                     /* see stat_.h */
  178.     pstatus->size_bytes = sbuf.st_size;
  179.     pstatus->time_referenced = sbuf.st_mtime;
  180.     pstatus->time_created = sbuf.st_ctime;
  181.     return 1;
  182. }
  183.  
  184. /* ------ File enumeration ------ */
  185.  
  186. /****** THIS IS NOT SUPPORTED ON UNIX SYSTEMS. ******/
  187. /* Amazingly enough, there is no standard Unix library routine */
  188. /* for enumerating the files matching a pattern, */
  189. /* or even for enumerating (conveniently) the files in a directory. */
  190.  
  191. struct file_enum_s {
  192.     char *pattern;
  193.     int first_time;
  194.     gs_memory_procs mprocs;
  195. };
  196.  
  197. /* Initialize an enumeration.  NEEDS WORK ON HANDLING * ? \. */
  198. file_enum *
  199. gp_enumerate_files_init(const char *pat, uint patlen,
  200.   proc_alloc_t palloc, proc_free_t pfree)
  201. {    file_enum *pfen = (file_enum *)(*palloc)(1, sizeof(file_enum), "gp_enumerate_files");
  202.     char *pattern;
  203.     if ( pfen == 0 ) return 0;
  204.     pattern = (*palloc)(patlen + 1, 1,
  205.                 "gp_enumerate_files(pattern)");
  206.     if ( pattern == 0 ) return 0;
  207.     memcpy(pattern, pat, patlen);
  208.     pattern[patlen] = 0;
  209.     pfen->pattern = pattern;
  210.     pfen->mprocs.alloc = palloc;
  211.     pfen->mprocs.free = pfree;
  212.     pfen->first_time = 1;
  213.     return pfen;
  214. }
  215.  
  216. /* Enumerate the next file. */
  217. uint
  218. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  219. {    if ( pfen->first_time )
  220.        {    pfen->first_time = 0;
  221.        }
  222.     return -1;
  223. }
  224.  
  225. /* Clean up the file enumeration. */
  226. void
  227. gp_enumerate_files_close(file_enum *pfen)
  228. {    proc_free_t pfree = pfen->mprocs.free;
  229.     (*pfree)(pfen->pattern, strlen(pfen->pattern) + 1, 1,
  230.          "gp_enumerate_files_close(pattern)");
  231.     (*pfree)((char *)pfen, 1, sizeof(file_enum), "gp_enumerate_files_close");
  232. }
  233.